using ASP;
using HoWave.Common;
using HoWave.Data;
using HoWave.Data.Bll;
using HoWave.Data.Model;
using HoWave.Web.Shop.Member;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class Member_StockManage_DepotList : Member, IRequiresSessionState
{
	private string stockOrderType = "";
	private string action = "";
	private int infoOrderID = 0;
	private int infoID = 0;
	private bool ownerpower = false;
	protected ScriptManager ScriptManager1;
	protected Literal ltrPageTitle;
	protected Literal ltrTitle;
	protected TextBox txtStockNumber;
	protected Label lblStockType;
	protected DropDownList ddlStockTypeStatue;
	protected TextBox txtInputTime;
	protected TextBox txtInputer;
	protected Label lblMoneyTotal;
	protected TextBox txtRemarks;
	protected HiddenField hfProductStr;
	protected HiddenField hfProductID;
	protected HiddenField hfProductIDCount;
	protected HiddenField hfItemID;
	protected HiddenField hfMoneyTotal;
	protected HtmlGenericControl divProduct;
	protected Button btnConfirm;
	protected Button btnDeleteInfo;
	protected Button btnSave;
	protected Button btnCancle;
	public string StockOrderType
	{
		get
		{
			if (base.Request.QueryString["stockType"] != null)
			{
				this.stockOrderType = base.Request.QueryString["stockType"].Trim();
			}
			return this.stockOrderType;
		}
	}
	public string Action
	{
		get
		{
			if (base.Request.QueryString["Action"] != null)
			{
				this.action = base.Request.QueryString["Action"].Trim();
			}
			else
			{
				this.action = "";
			}
			return this.action;
		}
		set
		{
			this.action = value;
		}
	}
	public int InfoOrderID
	{
		get
		{
			if (base.Request.QueryString["infoOrderID"] != null)
			{
				this.infoOrderID = Convert.ToInt32(base.Request.QueryString["infoOrderID"].Trim());
			}
			return this.infoOrderID;
		}
		set
		{
			this.infoOrderID = value;
		}
	}
	public int InfoID
	{
		get
		{
			if (this.InfoOrderID > 0)
			{
				DataTable dataTable = BLLInstance.BllAccountAoucher.GetList("StockOrderID=" + this.InfoOrderID).Tables[0];
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					this.infoID = Convert.ToInt32(dataTable.Rows[i]["AccountAoucherID"].ToString());
				}
			}
			return this.infoID;
		}
	}
	public bool Ownerpower
	{
		get
		{
			if (base.OwnerPowerFinancemanage(base.user.UserID))
			{
				this.ownerpower = true;
			}
			return this.ownerpower;
		}
	}
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.IsPostBack)
		{
			if (!base.OwnerStockPower(base.user.UserID))
			{
				base.WriteRightErrMsg();
			}
			if (this.StockOrderType == "bad")
			{
				this.ltrPageTitle.Text = "存货报损单";
			}
			else
			{
				if (this.StockOrderType == "baoyi")
				{
					this.ltrPageTitle.Text = "存货报溢单";
				}
			}
			this.GetStockType();
			string text = this.Action;
			if (text != null)
			{
				if (!(text == "addinfo"))
				{
					if (!(text == "editinfo"))
					{
						if (text == "confirminfo")
						{
							if (this.StockOrderType == "bad")
							{
								this.ltrTitle.Text = "确认存货报损单信息";
							}
							else
							{
								if (this.StockOrderType == "baoyi")
								{
									this.ltrTitle.Text = "确认存货报溢单信息";
								}
							}
							if (this.InfoOrderID > 0)
							{
								this.InitPage();
							}
							this.btnSave.Visible = false;
						}
					}
					else
					{
						this.btnSave.Text = "修改";
						if (this.StockOrderType == "bad")
						{
							this.ltrTitle.Text = "修改存货报损单";
						}
						else
						{
							if (this.StockOrderType == "baoyi")
							{
								this.ltrTitle.Text = "修改存货报溢单";
							}
						}
						if (this.InfoOrderID > 0)
						{
							this.InitPage();
						}
						this.btnConfirm.Visible = false;
						this.btnDeleteInfo.Visible = false;
					}
				}
				else
				{
					this.txtInputTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
					this.txtInputer.Text = base.user.UserName;
					this.btnConfirm.Visible = false;
					this.btnDeleteInfo.Visible = false;
					if (this.StockOrderType == "bad")
					{
						this.txtStockNumber.Text = "BS" + DateTime.Now.TimeString();
						this.ltrTitle.Text = "添加存货报损单";
					}
					else
					{
						if (this.StockOrderType == "baoyi")
						{
							this.txtStockNumber.Text = "BY" + DateTime.Now.TimeString();
							this.ltrTitle.Text = "添加存货报溢单";
						}
					}
				}
			}
		}
	}
	private void GetStockType()
	{
		string strWhere = "";
		if (this.StockOrderType == "bad")
		{
			this.lblStockType.Text = "出库类型：";
			strWhere = "StockTypeStatue='out'";
		}
		else
		{
			if (this.StockOrderType == "baoyi")
			{
				this.lblStockType.Text = "入库类型：";
				strWhere = "StockTypeStatue='in'";
			}
		}
		DataTable dataTable = BLLInstance.BllStockType.GetList(strWhere).Tables[0];
		foreach (DataRow dataRow in dataTable.Rows)
		{
			string value = dataRow["StockTypeID"].ToString();
			string text = dataRow["StockTypeName"].ToString();
			this.ddlStockTypeStatue.Items.Add(new ListItem(text, value));
		}
		this.ddlStockTypeStatue.Items.Insert(0, new ListItem("——请选择——", ""));
	}
	private bool CheckInput()
	{
		bool result = true;
		if (!string.IsNullOrEmpty(this.txtInputTime.Text))
		{
			if (!PageValidate.IsDateTime(this.txtInputTime.Text))
			{
				MessageBox.Show(this, "记录时间格式错误！");
				result = false;
			}
		}
		return result;
	}
	protected void InitPage()
	{
		HoWave.Data.Model.StockInfoOrder model = BLLInstance.BllStockInfoOrder.GetModel(this.InfoOrderID);
		if (model != null)
		{
			if (this.Action == "editinfo" || this.Action == "confirminfo")
			{
				this.txtStockNumber.Text = model.InfoNumber;
				this.ddlStockTypeStatue.ddlSelect(model.StocktTypeID.ToString());
				this.txtInputTime.Text = model.CreateDate.ToString();
				this.txtInputTime.Enabled = false;
				this.txtInputer.Text = model.Inputer;
				this.lblMoneyTotal.Text = model.TotalMoney.ToString();
				this.txtRemarks.Text = model.Remark;
				this.GetProductID(model.InfoOrderID);
			}
		}
	}
	private HoWave.Data.Model.StockInfoOrder GetStockInfoOrder()
	{
		HoWave.Data.Model.StockInfoOrder stockInfoOrder = new HoWave.Data.Model.StockInfoOrder();
		string text = this.txtStockNumber.Text;
		DateTime value = DateTime.Parse(this.txtInputTime.Text);
		string text2 = this.txtRemarks.Text;
		stockInfoOrder.InfoNumber = text;
		stockInfoOrder.StocktTypeID = new int?(Convert.ToInt32(this.ddlStockTypeStatue.SelectedValue));
		stockInfoOrder.CreateDate = new DateTime?(value);
		stockInfoOrder.Inputer = this.txtInputer.Text;
		string text3 = this.Action;
		if (text3 != null)
		{
			if (!(text3 == "addinfo"))
			{
				if (!(text3 == "editinfo"))
				{
					if (text3 == "confirminfo")
					{
						stockInfoOrder.UpdateDate = new DateTime?(DateTime.Now);
						if (this.StockOrderType == "bad")
						{
							stockInfoOrder.StockOrderType = this.StockOrderType;
							stockInfoOrder.StockStatus = new int?(3);
							stockInfoOrder.StockDate = new DateTime?(DateTime.Now);
						}
						else
						{
							if (this.StockOrderType == "baoyi")
							{
								stockInfoOrder.StockOrderType = this.StockOrderType;
								stockInfoOrder.StockStatus = new int?(1);
								stockInfoOrder.StockDate = new DateTime?(DateTime.Now);
							}
						}
					}
				}
				else
				{
					stockInfoOrder.UpdateDate = new DateTime?(DateTime.Now);
					if (this.StockOrderType == "bad")
					{
						stockInfoOrder.StockOrderType = this.StockOrderType;
						stockInfoOrder.StockStatus = new int?(2);
					}
					else
					{
						if (this.StockOrderType == "baoyi")
						{
							stockInfoOrder.StockOrderType = this.StockOrderType;
							stockInfoOrder.StockStatus = new int?(0);
						}
					}
				}
			}
			else
			{
				if (this.StockOrderType == "bad")
				{
					stockInfoOrder.StockOrderType = this.StockOrderType;
					stockInfoOrder.StockStatus = new int?(2);
					stockInfoOrder.StockDate = new DateTime?(DateTime.Now);
				}
				else
				{
					if (this.StockOrderType == "baoyi")
					{
						stockInfoOrder.StockOrderType = this.StockOrderType;
						stockInfoOrder.StockStatus = new int?(0);
						stockInfoOrder.StockDate = new DateTime?(DateTime.Now);
					}
				}
			}
		}
		decimal value2 = 0m;
		if (!string.IsNullOrEmpty(this.hfMoneyTotal.Value))
		{
			value2 = Convert.ToDecimal(this.hfMoneyTotal.Value);
		}
		stockInfoOrder.TotalMoney = new decimal?(value2);
		stockInfoOrder.OwnerID = base.user.UserID;
		stockInfoOrder.Remark = text2;
		return stockInfoOrder;
	}
	private DataTable GetStockOrderItemByInfoOrderID(int infoid)
	{
		string strWhere = "InfoOrderID =" + infoid;
		return BLLInstance.BllStockItemOrder.GetList(strWhere).Tables[0];
	}
	private void UpdateProductCount(string itemIDStr)
	{
		if (!string.IsNullOrEmpty(itemIDStr))
		{
			string[] array = itemIDStr.Split(new char[]
			{
				','
			});
			for (int i = 0; i < array.Length; i++)
			{
				int itemOrderID = Convert.ToInt32(array[i]);
				HoWave.Data.Model.StockItemOrder model = BLLInstance.BllStockItemOrder.GetModel(itemOrderID);
				if (model != null)
				{
					int num = Convert.ToInt32(model.ProductID.ToString());
					int num2 = Convert.ToInt32(model.StockCount.ToString());
					string text = "";
					if (this.StockOrderType == "bad")
					{
						text = "-";
					}
					else
					{
						if (this.StockOrderType == "baoyi")
						{
							text = "+";
						}
					}
					if (text != "")
					{
						string sql = string.Concat(new object[]
						{
							"update HLShop_Product set Stocks=Stocks",
							text,
							num2,
							" where ProductID=",
							num
						});
						this.conn.Execute(sql);
					}
				}
			}
		}
	}
	private void GetProductID(int infoid)
	{
		if (infoid > 0)
		{
			DataTable stockOrderItemByInfoOrderID = this.GetStockOrderItemByInfoOrderID(infoid);
			string text = "";
			string text2 = "";
			string text3 = "";
			for (int i = 0; i < stockOrderItemByInfoOrderID.Rows.Count; i++)
			{
				if (stockOrderItemByInfoOrderID.Rows[i]["ProductID"] != DBNull.Value)
				{
					string text4 = stockOrderItemByInfoOrderID.Rows[i]["ProductID"].ToString();
					string text5 = stockOrderItemByInfoOrderID.Rows[i]["StockCount"].ToString();
					text = text + text4 + ",";
					string text6 = text2;
					text2 = string.Concat(new string[]
					{
						text6,
						text4,
						",",
						text5,
						"|"
					});
					text3 = text3 + stockOrderItemByInfoOrderID.Rows[i]["ItemOrderID"].ToString() + ",";
				}
			}
			if (text != "")
			{
				this.hfProductID.Value = text.Substring(0, text.Length - 1);
				this.hfProductIDCount.Value = text2.Substring(0, text2.Length - 1);
			}
			if (text3 != "")
			{
				this.hfItemID.Value = text3.Substring(0, text3.Length - 1);
			}
		}
	}
	private int AddAccountAoucher(int stockOrderID, string infoNumber)
	{
		int result = 0;
		HoWave.Data.Model.AccountAoucher accountAoucher = new HoWave.Data.Model.AccountAoucher();
		accountAoucher.StockOrderID = new int?(stockOrderID);
		accountAoucher.AccountANumber = "JZPZ" + DateTime.Now.TimeString();
		accountAoucher.AccountACount = new int?(1);
		accountAoucher.AccountAExplain = "编号" + infoNumber + "的出库单记账凭证";
		accountAoucher.Inputer = base.user.UserName;
		accountAoucher.IsCaudit = true;
		accountAoucher.CauditName = base.user.UserName;
		accountAoucher.CauditDate = new DateTime?(DateTime.Now);
		accountAoucher.IsAccount = true;
		accountAoucher.AccountName = base.user.UserName;
		accountAoucher.AccountDate = new DateTime?(DateTime.Now);
		accountAoucher.InputDate = new DateTime?(DateTime.Now);
		accountAoucher.OwnerID = base.user.UserID;
		int num = BLLInstance.BllAccountAoucher.Add(accountAoucher);
		if (num > 0)
		{
			result = this.AAIAddBySell(num);
		}
		return result;
	}
	private int AAIAddBySell(int aainfoID)
	{
		int result = 0;
		decimal d = 0m;
		decimal moneyGoods = 0m;
		if (!string.IsNullOrEmpty(this.hfMoneyTotal.Value))
		{
			moneyGoods = Convert.ToDecimal(this.hfMoneyTotal.Value);
		}
		decimal deliver_TotalPrice = 0m;
		decimal invoice_TotalPrice = 0m;
		decimal num = 0m;
		decimal num2 = d + num;
		string text = this.Action;
		if (text != null)
		{
			if (!(text == "addinfo"))
			{
				if (text == "editinfo")
				{
					if (num > 0m)
					{
						bool flag = false;
						bool flag2 = false;
						DataTable dataTable = BLLInstance.BllAccountAoucherItem.GetList("AccountAoucherID=" + aainfoID).Tables[0];
						for (int i = 0; i < dataTable.Rows.Count; i++)
						{
							int accountAItemID = Convert.ToInt32(dataTable.Rows[i]["AccountAItemID"].ToString());
							int accountSubjectID = Convert.ToInt32(dataTable.Rows[i]["AAItemAccountSID"].ToString());
							HoWave.Data.Model.AccountAoucherItem model = BLLInstance.BllAccountAoucherItem.GetModel(accountAItemID);
							if (model != null)
							{
								model.AccountAItemID = accountAItemID;
								HoWave.Data.Model.AccountSubject model2 = BLLInstance.BllAccountSubject.GetModel(accountSubjectID);
								if (model2 != null)
								{
									string a = model2.AccountSName.Trim();
									if (a == "应收账款")
									{
										model.BorrowPrice = new decimal?(num2);
										BLLInstance.BllAccountAoucherItem.Update(model);
										flag2 = true;
									}
									else
									{
										if (a == "手续费")
										{
											model.LoanPrice = new decimal?(num);
											BLLInstance.BllAccountAoucherItem.Update(model);
											flag = true;
										}
										else
										{
											if (a == "商品销售收入" || a == "运输费" || a == "发票费")
											{
												flag2 = true;
											}
										}
									}
								}
							}
						}
						if (flag2 && !flag)
						{
							BLLInstance.BllAccountAoucherItem.AddAccountAoucherItem(aainfoID, 0m, num, "销售出库单", "手续费");
						}
					}
				}
			}
			else
			{
				result = BLLInstance.BllAccountAoucherItem.AccountAoucherItemAddBySell(aainfoID, num2, moneyGoods, deliver_TotalPrice, invoice_TotalPrice, num);
			}
		}
		return result;
	}
	protected void btnSave_Click(object sender, EventArgs e)
	{
		if (this.CheckInput())
		{
			HoWave.Data.Model.StockInfoOrder stockInfoOrder = this.GetStockInfoOrder();
			string comeUrl = "DepotListManage.aspx?stockType=" + this.StockOrderType;
			string text = this.Action;
			if (text != null)
			{
				if (!(text == "addinfo"))
				{
					if (text == "editinfo")
					{
						HoWave.Data.Model.StockInfoOrder stockInfoOrder2 = BLLInstance.BllStockInfoOrder.GetModel(this.InfoOrderID);
						if (stockInfoOrder2 != null)
						{
							stockInfoOrder2 = stockInfoOrder;
							stockInfoOrder2.InfoOrderID = this.InfoOrderID;
							bool flag = BLLInstance.BllStockInfoOrder.Update(stockInfoOrder2);
							if (flag)
							{
								bool flag2 = BLLInstance.BllStockItemOrder.EditStockOrderItem(this.hfItemID.Value, this.hfProductStr.Value);
								if (flag2)
								{
									this.WriteSuccessMsg("", "信息修改成功！", comeUrl);
								}
							}
							else
							{
								this.WriteErrMsg("", "信息修改失败！", comeUrl);
							}
						}
						stockInfoOrder.InfoOrderID = this.InfoOrderID;
					}
				}
				else
				{
					int num = BLLInstance.BllStockInfoOrder.Add(stockInfoOrder);
					if (num > 0)
					{
						int num2 = BLLInstance.BllStockItemOrder.AddStockOrderItem(num, this.txtStockNumber.Text, this.hfProductStr.Value);
						if (num2 > 0)
						{
							this.WriteSuccessMsg("", "添加信息成功；请您重新检查信息是否有误，确认信息！", comeUrl);
						}
					}
					else
					{
						this.WriteErrMsg("", "添加信息失败！", "");
					}
				}
			}
		}
	}
	protected void btnConfirm_Click(object sender, EventArgs e)
	{
		string comeUrl = "DepotListManage.aspx?stockType=" + this.StockOrderType;
		HoWave.Data.Model.StockInfoOrder stockInfoOrder = BLLInstance.BllStockInfoOrder.GetModel(this.InfoOrderID);
		if (stockInfoOrder != null)
		{
			HoWave.Data.Model.StockInfoOrder stockInfoOrder2 = this.GetStockInfoOrder();
			stockInfoOrder = stockInfoOrder2;
			stockInfoOrder.InfoOrderID = this.InfoOrderID;
			bool flag = BLLInstance.BllStockInfoOrder.Update(stockInfoOrder);
			if (flag)
			{
				bool flag2 = BLLInstance.BllStockItemOrder.EditStockOrderItem(this.hfItemID.Value, this.hfProductStr.Value);
				if (flag2)
				{
					this.UpdateProductCount(this.hfItemID.Value);
					this.WriteSuccessMsg("", "信息确认成功！", comeUrl);
				}
			}
			else
			{
				this.WriteErrMsg("", "信息确认失败！", comeUrl);
			}
		}
	}
	protected void btnDeleteInfo_Click(object sender, EventArgs e)
	{
		string comeUrl = "DepotListManage.aspx?stockType=" + this.StockOrderType;
		if (this.StockOrderType == "bad")
		{
			bool flag = this.DelStockInfoAndItem(Convert.ToInt32(this.InfoOrderID), this.StockOrderType);
			if (flag)
			{
				this.WriteSuccessMsg("", "信息删除成功！", comeUrl);
			}
			else
			{
				this.WriteErrMsg("", "信息删除失败！", comeUrl);
			}
		}
	}
	private bool DelStockInfoAndItem(int infoid, string stockOrderType)
	{
		bool flag = false;
		if (infoid > 0)
		{
			HoWave.Data.Model.StockInfoOrder model = BLLInstance.BllStockInfoOrder.GetModel(infoid);
			if (model != null)
			{
				DataTable dataTable = BLLInstance.BllStockItemOrder.GetList("InfoOrderID=" + infoid).Tables[0];
				foreach (DataRow dataRow in dataTable.Rows)
				{
					int num = Convert.ToInt32(dataRow["ItemOrderID"].ToString());
					int num2 = Convert.ToInt32(dataRow["ProductID"].ToString());
					int num3 = Convert.ToInt32(dataRow["StockCount"].ToString());
					if (num > 0)
					{
						flag = BLLInstance.BllStockItemOrder.Delete(num);
						if (flag)
						{
							if (model.StockOrderType == "bad")
							{
								if (model.StockStatus == 3)
								{
									string sql = string.Concat(new object[]
									{
										"update HLShop_Product set Stocks=Stocks+",
										num3,
										" where ProductID=",
										num2
									});
									this.conn.Execute(sql);
								}
							}
						}
					}
				}
				flag = BLLInstance.BllStockInfoOrder.Delete(infoid);
			}
		}
		return flag;
	}
	public void btnCancle_Click(object sender, EventArgs e)
	{
		base.Response.Redirect("DepotListManage.aspx?stockType=" + this.StockOrderType);
	}
}
